package com.haixiaoke.saas.enterprise.house.controller;

import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.house.domain.Estate;
import com.haixiaoke.saas.house.service.IEstateService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.common.Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 楼盘信息Controller
 *
 * @author xiaolong
 * @date 2023-10-16
 */
@RestController
@RequestMapping("/enterprise/house/estate")
public class EnterpriseEstateController extends BaseController {
    @Autowired
    private IEstateService estateService;

    /**
     * 查询楼盘信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:estate:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(Estate estate) {
        startPage();
        estate.setTenantId(TenantContext.getTenantId());
        List<Estate> list = estateService.selectEstateListInfo(estate);
        return getDataTable(list);
    }

    /**
     * 查询楼盘信息列表
     */
    @GetMapping("/estateList")
    @TenantScope
    public AjaxResult estateList(Estate estate) {
        estate.setTenantId(TenantContext.getTenantId());
        List<Estate> list = estateService.selectEstateList(estate);
        return success(list);
    }

    @GetMapping("/level/list")
    @TenantScope
    public List<Node> levelList(Estate estate) {
        estate.setTenantId(TenantContext.getTenantId());
        List<Node> list = estateService.selectEstateLevelList(estate);
        return list;
    }


    /**
     * 导出楼盘信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:estate:export')")
    @PostMapping("/export")
    @TenantScope
    public void export(HttpServletResponse response, Estate estate) {
        estate.setTenantId(TenantContext.getTenantId());
        List<Estate> list = estateService.selectEstateList(estate);
        ExcelUtil<Estate> util = new ExcelUtil<Estate>(Estate.class);
        util.hideColumn("provinceId","cityId","districtId","streetId","communityId","propertyId");
        util.exportExcel(response, list, "楼盘信息数据");
    }

    /**
     * 获取楼盘信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('house:estate:query')")
    @GetMapping(value = "/{estateId}")
    public AjaxResult getInfo(@PathVariable("estateId") String estateId) {
        return success(estateService.selectEstateByEstateId(estateId));
    }

    /**
     * 新增楼盘信息
     */
    @PreAuthorize("@ss.hasPermi('house:estate:add')")
    @PostMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult add(@RequestBody @Valid Estate estate) {
        Long userId = getUserId();
        estate.setTenantId(TenantContext.getTenantId());
        return toAjax(estateService.insertEstate(estate,userId));
    }

    /**
     * 修改楼盘信息
     */
    @PreAuthorize("@ss.hasPermi('house:estate:edit')")
    @PutMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult edit(@RequestBody Estate estate) {
        return toAjax(estateService.updateEstate(estate));
    }

    /**
     * 删除楼盘信息
     */
    @PreAuthorize("@ss.hasPermi('house:estate:remove')")
    @DeleteMapping("/remove/{estateIds}")
    @TenantScope
    @RepeatSubmit
    public AjaxResult remove(@PathVariable String[] estateIds) {
        return toAjax(estateService.deleteEstateByEstateIds(estateIds));
    }

    /**
     * 统计楼盘数据
     */
    @GetMapping(value = "/statistics")
    @TenantScope
    public AjaxResult getStatistics() {
        return success(estateService.getStatistics());
    }
}
